<template>
  <div>
 <!-- 面包屑 -->
 <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
</el-breadcrumb>

<el-card>
  <el-row :gutter="20">
    <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getTableDate">
            <el-button slot="append" icon="el-icon-search" @click="getTableDate"></el-button>
        </el-input>
    </el-col>
    <el-col :span="4">
      <el-button type="primary" @click="addGoods">添加商品</el-button>
    </el-col>
  </el-row>
     <el-table
      :data="tableData" border stripe>
      <el-table-column type="index">
      </el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格(元)"
        type="number"
        width="120">
      </el-table-column>
      <el-table-column
        prop="goods_weight"
        label="重量"
        type="number"
        width="80">
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="add_time"
        width="180">
        <template slot-scope="scope">
          {{scope.row.add_time | dateFormat}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
       <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="bj1Form(scope.row.goods_id)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="scbtn(scope.row.goods_id)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 15, 20, 30]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>

<!-- 编辑商品对话框 -->
<el-dialog title="编辑商品" :visible.sync="bjFormVisible" width="50%" @close="guanbi">
  <el-form label-width="80px" :model="bjForm" :rules="ruless" ref="bjFormRef">
    <el-form-item label="商品名称：" prop="goods_name" label-width="100px">
    <el-input v-model="bjForm.goods_name"></el-input>
    </el-form-item >
    <el-form-item label="商品价格：" type="number" prop="goods_price" label-width="100px">
    <el-input v-model="bjForm.goods_price"></el-input>
    </el-form-item >
    <el-form-item label="重量：" type="number"  prop="goods_weight" label-width="100px">
    <el-input v-model="bjForm.goods_weight"></el-input>
    </el-form-item >
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="guanbi">取 消</el-button>
    <el-button type="primary" @click="adbtn">确 定</el-button>
  </div>
</el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      bjForm: {},
      bjFormVisible: false,
      ruless: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getTableDate()
  },
  methods: {
    async getTableDate () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.total = res.data.total
      this.tableData = res.data.goods
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getTableDate()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getTableDate()
    },
    addGoods () {
      this.$router.push('/goods/add')
    },
    async scbtn (id) {
      // 根据id删除对应的参数或属性
      // 弹窗提示用户是否要删除
      const confirmResult = await this.$confirm(
        '请问是否要删除该商品',
        '删除提示',
        {
          confirmButtonText: '确认删除',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果用户点击确认，则confirmResult 为'confirm'
      // 如果用户点击取消, 则confirmResult获取的就是catch的错误消息'cancel'
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }

      // 没有取消就是要删除，发送请求完成删除
      const { data: res } = await this.$http.delete(`goods/${id}`)

      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }

      this.$message.success('删除商品成功')
      this.getTableDate()
    },
    adbtn () {
      this.$refs.bjFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('goods/', this.bjForm)
        if (res.meta.status !== 201) return this.$message.error('添加数据失败')
        this.getList()
        this.bjFormVisible = false
      })
    },
    async bj1Form (id) {
      this.bjFormVisible = true
      const { data: res } = await this.$http.get(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('获取商品信息失败')
      this.bjForm = res.data
    },
    guanbi () {
      this.$refs.bjFormRef.resetFields()
      this.bjFormVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.el-pagination {
  text-align: center;
}
.el-row {
  margin-bottom: 20px
}
</style>
